package edu.zut.cs.software.person.supplier.service;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import edu.zut.cs.software.person.supplier.domain.Supplier;
import edu.zut.cs.software.teach.Base.service.GenericGenerator;

public class SupplierEntityGenerator extends GenericGenerator{
	
	List<Supplier> supplierList;
	
	SupplierManager suppliermanager;
	
	@Autowired
	public void setSuppliermanger(SupplierManager suppliermanager) {
		this.suppliermanager = suppliermanager;
	}
	@Before
	public void setUp() throws Exception {
		this.supplierList = new ArrayList<Supplier>();
		InputStream input = SupplierManagerTest.class.getResourceAsStream("/sms_supplier_software.xlsx");
		@SuppressWarnings("resource")
		XSSFWorkbook wb = new XSSFWorkbook(input);
		XSSFSheet sheet = wb.getSheetAt(0);
		DataFormatter dataFormatter = new DataFormatter();
		for (int  i=1;i <= sheet.getLastRowNum(); i++) {
			Row row = sheet.getRow(i);
			Supplier s = new Supplier();
			for (int j= 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					String value = dataFormatter.formatCellValue(cell);
					if (j == 1)
						s.setName(value);
				}
			}
			this.supplierList.add(s);
		}
	}
	@Test
	public void test() {
		this.suppliermanager.deleteAll();
		this.suppliermanager.save(this.supplierList);
		Iterable<Supplier> result = this.suppliermanager.findAll();
		if (logger.isInfoEnabled()) {
			logger.info("test() - List<Supplier> result=" + result); //$NON-NLS-1$
		}
	}
}
